﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace Spider
{
    class Log
    {
        private static Log mLog;
        private static System.Diagnostics.EventLog eventLog;
        string logFileName = @"log.txt";

        public static Log Instance
        {
            get
            {
                if (mLog == null)
                    mLog = new Log();
                CreateLog();
                return mLog;
            }
        }

        private static void CreateLog()
        {
            eventLog = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("Crawl"))
            {
                System.Diagnostics.EventLog.CreateEventSource("Crawl", "Crawl");
            }
            eventLog.Source = "Crawl";
            eventLog.Log = "Crawl";
        }

        public void WriteLog(string LogInfo)
        {
            eventLog.WriteEntry(LogInfo, System.Diagnostics.EventLogEntryType.Information);
        }

        public void WriteException(Exception ex)
        {
            Monitor.Enter(this);
            string info = string.Format("Exception Info:{0}         Exception StackTrace:{1}", ex.Message, ex.StackTrace);
            
            System.IO.StreamWriter  write=null;
            if (!System.IO.File.Exists(logFileName))
                write = System.IO.File.CreateText(logFileName);
            else
                write = System.IO.File.AppendText(logFileName);

            write.WriteLine(info);

            write.Flush();

            write.Close();

            write.Dispose();

            Monitor.Pulse(this);

            Monitor.Exit(this);
            
            //eventLog.WriteEntry(info, System.Diagnostics.EventLogEntryType.Error);
        }

        public void WriteException(string ErrorMsg)
        {
            Monitor.Enter(this);

            System.IO.StreamWriter write = null;

            if (!System.IO.File.Exists(logFileName))
                write = System.IO.File.CreateText(logFileName);
            else
                write = System.IO.File.AppendText(logFileName);

            write.WriteLine(ErrorMsg);

            write.Flush();

            write.Close();

            write.Dispose();

            Monitor.Pulse(this);

            Monitor.Exit(this);

            //eventLog.WriteEntry(info, System.Diagnostics.EventLogEntryType.Error);
        }

        public void WriteMsg(string Msg)
        {
            Monitor.Enter(this);

            System.IO.StreamWriter write = null;

            if (!System.IO.File.Exists(@"msg.txt"))
                write = System.IO.File.CreateText(@"msg.txt");
            else
                write = System.IO.File.AppendText(@"msg.txt");

            write.WriteLine(Msg);

            write.Flush();

            write.Close();

            write.Dispose();

            Monitor.Pulse(this);

            Monitor.Exit(this);

            //eventLog.WriteEntry(info, System.Diagnostics.EventLogEntryType.Error);
        }


        public void OpenLogFile()
        {
            if (File.Exists(logFileName))
            {
                ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("notepad.exe", logFileName);
                Process p = new System.Diagnostics.Process();
                p.StartInfo = info;
                p.Start();
            }
        }
    }


    class RequestException
    {

    }
}
